//
//  BCCustomerServiceController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/16.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCCustomerServiceController.h"
//
#import "BCustomerServiceRegionHeaderView.h"
#import "BCustomerServiceRegionFooterView.h"
#import "BCustomerServiceCell.h"

//
#import "BCustomerServiceTableFooterView.h"

//打开在线客服的控制器
#import "BCCustomerOnlineController.h"

@interface BCCustomerServiceController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *serviceTable;


/**
 *BCustomerServiceData
 */
@property (nonatomic, strong) BCustomerServiceData *serviceData;

/**
 *BCustomerServiceTableFooterView
 */
@property (nonatomic, strong) BCustomerServiceTableFooterView *serviceFooter;

@end

@implementation BCCustomerServiceController

- (BCustomerServiceTableFooterView *)serviceFooter {
    if (!_serviceFooter) {
        _serviceFooter = [[BCustomerServiceTableFooterView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)];
        [_serviceFooter.serviceButton addTarget:self action:@selector(toOnlineController) forControlEvents:UIControlEventTouchUpInside];
    }
    return _serviceFooter;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    [self setupSubviews];
    
    //
    BCustomerServiceData *data;
    if (self.serviceType==BCustomerServiceTypeService) {
        data = [BCCustomerServiceManager sharedManager].serviceData;
    }else if (self.serviceType==BCustomerServiceTypeFeedback) {
        data = [BCCustomerServiceManager sharedManager].feedbackData;
    }
    if (data) {
        [self handleData:data init:true refresh:false];
        [self requestDataInit:false refresh:true];
    }else {
        [self requestDataInit:true refresh:false];
    }
}

- (void)setupSubviews {
    //
    self.view.backgroundColor = [UIColor qmui_colorWithHexString:@"#F8F8F8"];
    self.serviceTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.serviceTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.serviceTable.backgroundColor = [UIColor clearColor];
    self.serviceTable.dataSource = self;
    self.serviceTable.delegate = self;
    [self.view addSubview:self.serviceTable];
    [self.serviceTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.customNaviBar.mas_bottom);
        make.left.bottom.right.mas_equalTo(self.view);
    }];
    //
    [self.serviceTable registerClass:BCustomerServiceCell.class forCellReuseIdentifier:NSStringFromClass(BCustomerServiceCell.class)];
    [self.serviceTable registerClass:BCustomerServiceRegionHeaderView.class forHeaderFooterViewReuseIdentifier:NSStringFromClass(BCustomerServiceRegionHeaderView.class)];
    [self.serviceTable registerClass:BCustomerServiceRegionFooterView.class forHeaderFooterViewReuseIdentifier:NSStringFromClass(BCustomerServiceRegionFooterView.class)];
    //
    [self.view layoutIfNeeded];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.serviceData.data.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    BCustomerServiceDataModel *dataModel = self.serviceData.data[section];
    return dataModel.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BCustomerServiceDataModel *dataModel = self.serviceData.data[indexPath.section];
    BCustomerServiceDataItem *item = dataModel.data[indexPath.row];
    BCustomerServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(BCustomerServiceCell.class)];
    cell.dataItem = item;
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    BCustomerServiceDataModel *dataModel = self.serviceData.data[section];
    BCustomerServiceRegionHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(BCustomerServiceRegionHeaderView.class)];
    headerView.title = dataModel.name;
    return headerView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    BCustomerServiceRegionFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(BCustomerServiceRegionFooterView.class)];
    return footer;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 25.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 20.0;
}
//
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BCustomerServiceDataModel *dataModel = self.serviceData.data[indexPath.section];
    BCustomerServiceDataItem *item = dataModel.data[indexPath.row];
    if (item) {
        WEAK_SELF
        UIAlertController *actionVC = [UIAlertController alertControllerWithTitle:item.value message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *callAction = [UIAlertAction actionWithTitle:self.serviceData.flow.call style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [UIApplication callPhone:item.value];
        }];
        UIAlertAction *copyAction = [UIAlertAction actionWithTitle:self.serviceData.flow.charge_copy style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UIPasteboard *paste = [UIPasteboard generalPasteboard];
            [paste setString:item.value];
            //
            NSString *success = BCLocalizedKey(@"general_copy_success");
            [QMUITips showSucceed:success inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:success]];
        }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:BCLocalizedKey(@"Cancel") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        if (item.is_call) {
            [actionVC addAction:callAction];
        }
        [actionVC addAction:copyAction];
        [actionVC addAction:cancelAction];
        [self presentViewController:actionVC animated:true completion:nil];
    }
}



//reqeust
- (void)requestDataInit:(BOOL)init refresh:(BOOL)refresh {
    if (init) {
        [self.view bc_showLoadingInsets:UIEdgeInsetsMake(CGRectGetHeight(self.customNaviBar.frame), 0, 0, 0)];
    }
    
    BCustomerServiceRequest *request = [BCustomerServiceRequest new];
    request.serviceType = self.serviceType;
    //
    WEAK_SELF
    [request didRequestCompleted:^{
        if (init) {
            [wself.view bc_hiddenLoading];
        }
    }];
    [request didRequestSuccessed:^(id result, id page) {
        BCustomerServiceResponse *resp = result;
        [wself handleData:resp.data init:init refresh:refresh];
    }];
    [request didRequestFailed:^(NSError *error) {
        if (init&&wself.view) {
            [wself.serviceTable bc_showError:error needRefresh:true refreshTitle:nil target:wself refreshAction:@selector(requestInitFailedAction)];
        }
    }];
    [request requestServer];
}
- (void)requestInitFailedAction {
    [self.serviceTable bc_hiddenError];
    [self requestDataInit:true refresh:false];
}
//
- (void)handleData:(BCustomerServiceData *)respData init:(BOOL)init refresh:(BOOL)refresh {
    if (respData) {
        self.isLoadedData = true;
        //
        if (self.serviceType==BCustomerServiceTypeService) {
            [BCCustomerServiceManager sharedManager].serviceData = respData;
        }else if (self.serviceType==BCustomerServiceTypeFeedback) {
            [BCCustomerServiceManager sharedManager].feedbackData = respData;
        }
        //
        self.title = respData.flow.title;
        //table数据
        self.serviceData = respData;
        
        //在线按钮显示与否
        if (respData.sms_url.length) {
            [self.serviceTable setTableFooterView:self.serviceFooter];
        }else {
            [self.serviceTable setTableFooterView:nil];
        }
    }
    [self.serviceTable reloadData];
}



#pragma BCCustomerOnlineController
- (void)toOnlineController {
    BCCustomerOnlineController *onLineVC = [BCCustomerOnlineController new];
    onLineVC.sms_url = self.serviceData.sms_url;
    [self presentViewController:onLineVC animated:true completion:nil];
}

@end
